/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QFaces.
 * QFaces is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QFaces is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QFaces.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QFaces的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QFaces是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QFaces的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看并获取副本。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

package name.huliqing.qfaces.component;

//import java.util.logging.Logger;
import javax.el.ValueExpression;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;

public class DynFrame extends UIComponentBase {
//	private final static Logger logger = Logger.getLogger(Frame.class.getName());

    // 是否自动将Frame居中,如果为true,则组件在显示时将出现在屏幕中间
    private Boolean center;
    
    // 是否使用遮罩层
    private Boolean shade;
    
    // 遮罩层的颜色,如：red,green,blue,#000000,#CCCCCC等表示颜色的值都可以
    private String shadeColor;
    
    // 遮罩层的不透明度,取值只能是 0.0 ~ 1.0之间(包括0.0, 1.0)
    private Float shadeOpaque;
    
    // Event, 绑定到JavaScript Function, 当窗口打开时执行的JS函数
    // 如果该函数返回false,则窗口将不打开
    private String onOpen;
    
    // Event, 绑定到JavaScript Function, 当窗口关闭时执行的JS函数
    // 如果该函数返回false,则窗口将不关闭
    private String onClose;
    
    // 内部Frame工具
    private Frame frame;
    
    private String loadType;
    
    public DynFrame() {
    	super();
    	setRendererType("name.huliqing.qfaces.render.DynFrameRenderer");
    }
    
    @Override
    public String getFamily() {
    	return "name.huliqing.qfaces.render.DynFrameFamily";
    }

    private Object[] _values;
    @Override
    public void restoreState(FacesContext fc, Object state) {
        _values = (Object[]) state;
        super.restoreState(fc, _values[0]);
        this.center = (Boolean) _values[1];
        this.shade = (Boolean) _values[2];
        this.shadeColor = (String) _values[3];
        this.shadeOpaque = (Float) _values[4];
        this.onOpen = (String) _values[5];
        this.onClose = (String) _values[6];
        this.frame = (Frame) _values[7];
        this.loadType = (String) _values[8];
    }

    @Override
    public Object saveState(FacesContext fc) {
    	if (_values == null)
    		_values = new Object[9];
        _values[0] = super.saveState(fc);
        _values[1] = this.center;
        _values[2] = this.shade;
        _values[3] = this.shadeColor;
        _values[4] = this.shadeOpaque;
        _values[5] = this.onOpen;
        _values[6] = this.onClose;
        _values[7] = this.frame;
        _values[8] = this.loadType;
        return _values;
    }

    private Object getFromExp(String name) {
		ValueExpression _ve = getValueExpression(name);
        if (_ve != null)
            return _ve.getValue(getFacesContext().getELContext());
        else
            return null;
	}
    
    // ---- Getter and Setter
    
	public Boolean getCenter() {
		if (center != null)
			return center;
		return (Boolean) getFromExp("center");
	}

	public void setCenter(Boolean center) {
		this.center = center;
	}

	public Boolean getShade() {
		if (shade != null)
			return shade;
		return (Boolean) getFromExp("shade");
	}

	public void setShade(Boolean shade) {
		this.shade = shade;
	}

	public String getShadeColor() {
		if (shadeColor != null)
			return shadeColor;
		return (String) getFromExp("shadeColor");
	}

	public void setShadeColor(String shadeColor) {
		this.shadeColor = shadeColor;
	}

	public Float getShadeOpaque() {
		if (shadeOpaque != null)
			return shadeOpaque;
		return (Float) getFromExp("shadeOpaque");
	}

	public void setShadeOpaque(Float shadeOpaque) {
		this.shadeOpaque = shadeOpaque;
	}

	public String getOnOpen() {
		if (onOpen != null)
			return onOpen;
		return (String) getFromExp("onOpen");
	}

	public void setOnOpen(String onOpen) {
		this.onOpen = onOpen;
	}

	public String getOnClose() {
		if (onClose != null)
			return onClose;
		return (String) getFromExp("onClose");
	}

	public void setOnClose(String onClose) {
		this.onClose = onClose;
	}

	public Frame getFrame() {
		// frame内部组件不能从ValueExpression中绑定
		return frame;
	}

	public void setFrame(Frame frame) {
		this.frame = frame;
	}
	
	public String getLoadType() {
		if (loadType != null)
			return loadType;
		return (String) this.getFromExp("loadType");
	}
	
	public void setLoadType(String loadType) {
		this.loadType = loadType;
	}
	
}
